﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Text;
using System.Runtime.InteropServices;
namespace FRMS
{
    public class FSInvokeFun 
    {
        /// <summary>
        /// 您将需要在网站的 Web.config 文件中配置此模块
        /// 并向 IIS 注册它，然后才能使用它。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpModule Members

        public void Dispose()
        {
            //此处放置清除代码。
        }

        //public void Init(HttpApplication context)
        //{
        //    // 下面是如何处理 LogRequest 事件并为其 
        //    // 提供自定义日志记录实现的示例
        //    context.LogRequest += new EventHandler(OnLogRequest);
        //}

        #endregion

        public void OnLogRequest(Object source, EventArgs e)
        {
            //可以在此处放置自定义日志记录逻辑
        }
        public static string CheckErr(byte st)
        {
            string result = "";
            switch (st)
            {
                case 1: result = "在有效区域内没有检测到卡"; break;
                case 2: result = "从卡中接收到了错误的CRC校验和"; break;
                case 3: result = "值溢出"; break;
                case 4: result = "验证错误"; break;
                case 5: result = "从卡中接收到了错误的校验位"; break;
                case 6: result = "通信错误"; break;
                case 8: result = "在防冲突时读到了错误的串行码"; break;
                case 10: result = "卡没有验证"; break;
                case 11: result = "从卡中接收到了错误数量的位"; break;
                case 12: result = "从卡中接收了错误数量的字节"; break;
                case 14: result = "调用Transfer函数出错"; break;
                case 15: result = "调用Write函数出错"; break;
                case 16: result = "调用Increment函数出错"; break;
                case 17: result = "调用Decrment函数出错"; break;
                case 18: result = "调用Read函数出错"; break;
                case 24: result = "卡冲突错"; break;
                case 30: result = "上一次了送命令时被打断"; break;
                case 255: result = "串行通信错误"; break;
                default: result = "对不起，未知错误代码"; break;
            }
            return result;
        }
        /// <summary>
        /// 发送请求命令(Request)，检查在有效范围内是否有卡的存在
        /// </summary>
        /// <param name="mode">请求类型  (mode=0:请求天线范围内IDLE状态的卡（HALT状态的除外）mode=1: 请求天线范围内的所有卡)</param>
        /// <param name="tagtype">返回的目标类型</param>
        /// <returns>0 操作成功、非0发操作失败</returns>
        [DllImport("FS500USB.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern byte RC500USB_request(byte mode, ref ushort tagtype);

        /// <summary>
        /// 开始防冲突操作
        /// </summary>
        /// <param name="bcnt">为预选卡所分配的位的个数，通常Bcnt=0、</param>
        /// <param name="snr">返回卡的序号</param>
        /// <returns>0 操作成功、非0发操作失败</returns>
        [DllImport("FS500USB.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern byte RC500USB_anticoll(byte bcnt, ref UInt32 snr);

        /// <summary>
        /// 选择某一个序号的mf卡
        /// </summary>
        /// <param name="snr">卡的序号</param>
        /// <param name="size">返回卡的容量</param>
        /// <returns> 0 操作成功、非0发操作失败</returns>
        [DllImport("FS500USB.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern byte RC500USB_select(UInt32 snr, ref byte size);

        /// <summary>
        /// 将RC500中的密匙与卡中的密匙进行验证(每个扇区密码相同)
        /// (在进行对卡片的读写操作之前，必须成功执行此指令。
        /// 系统初始化时，主控设备将把每个扇区的密码加载到读卡器中。
        /// </summary>
        /// <param name="mode">密钥类型,密钥A:0x00,密钥B:0x04</param>
        /// <param name="secnr">要验证的卡扇区号</param>
        /// <returns>0 操作成功、非0发操作失败</returns>
        [DllImport("FS500USB.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern byte RC500USB_authentication(byte mode, UInt32 secnr);

        /// <summary>
        /// 密钥验证(在对卡进行读、写、加、减等操作前)
        /// </summary>
        /// <param name="mode">密钥类型,密钥A:0x00,密钥B:0x04</param>
        /// <param name="key">存储密钥缓冲区</param>
        /// <param name="secnr">要验证的卡扇区号</param>
        /// <returns> 0 操作成功、非0发操作失败</returns>
        [DllImport("FS500USB.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern byte RC500USB_authkey(byte mode, byte[] key, byte secnr);

        [DllImport("FS500USB.dll")]
        public static extern byte RC500USB_halt();

        /// <summary>
        /// 读取卡的一个块的数据
        /// </summary>
        /// <param name="addr">块号(0x00--0x3f)</param>
        /// <param name="data">块数据，长度16个字节</param>
        /// <returns>0 操作成功、非0发操作失败</returns>
        [DllImport("FS500USB.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern byte RC500USB_read(byte addr, ref byte data);

        /// <summary>
        /// 对指定的块进行整块写操作。
        /// </summary>
        /// <param name="addr">块号(0x00--0x3f)</param>
        /// <param name="data">块数据，长度16个字节</param>
        /// <returns>0 操作成功、非0发操作失败</returns>
        [DllImport("FS500USB.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern byte RC500USB_write(byte addr, ref byte data);

        /// <summary>
        /// RC500中的密匙与卡中的密匙进行验证(全部密匙相同)
        /// </summary>
        /// <param name="mode">密钥类型,密钥A:0x00,密钥B:0x04</param>
        /// <param name="secnr">要载入的读卡器中的密钥区号</param>
        /// <param name="key">密码，长度6个字节</param>
        /// <returns>0 操作成功、非0发操作失败</returns>
        [DllImport("FS500USB.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern byte RC500USB_load_key(byte mode, byte secnr, byte key);

        /// <summary>
        /// 系统配制(模块每次上电复位之后，都必须首先调用此函数 对模块进行初始化，才能进行进一步的操作。)
        /// </summary>
        /// <returns>0 操作成功、非0发操作失败</returns>
        [DllImport("FS500USB.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern byte RC500USB_config();

        /// <summary>
        /// 此函数输出一驱动信号可驱动蜂鸣器和绿色发光管，持续时间、间隙时间和重复次数可调
        /// </summary>
        /// <param name="contrl">控制字，如下表相应位为1时该器件动作。contrl^1为绿灯,contrl^0为蜂鸣器</param>
        /// <param name="opentm">低电平持续时间，取值（0-255），10ms的分辨率</param>
        /// <param name="closetm">高电平间隙时间，取值（0-255），10ms的分辨率</param>
        /// <param name="repcnt">重复次数</param>
        /// <returns>0 操作成功、非0发操作失败</returns>
        [DllImport("FS500USB.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern byte RC500USB_buzzer(byte contrl, byte opentm, byte closetm, byte repcnt);

        /// <summary>
        /// 初始化MUR-100读卡器
        /// </summary>
        /// <returns>无</returns>
        [DllImport("FS500USB.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern byte RC500USB_init();

        /// <summary>
        /// 关闭MUR-100读卡器
        /// </summary>
        ///<returns>无</returns>
        [DllImport("FS500USB.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void RC500USB_exit();
    }
}
